package the.broccoli.generator.code.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import the.broccoli.generator.code.service.ITableService;
import the.broccoli.generator.engine.WistEngine;
import the.broccoli.generator.engine.datasource.DruidProperties;
import the.broccoli.generator.engine.po.WistData;
import the.broccoli.generator.engine.po.WistField;

import java.util.List;

@RestController
@RequestMapping("/generator/code")
public class CodeController {

    @Autowired
    private DruidProperties druidProperties;

    @Autowired
    private ITableService tableService;

    @GetMapping("/tables")
    public APIResult getTables() {
        return APIResult.success(tableService.getTables());
    }

    @GetMapping("/table/fields")
    public APIResult getFields(@RequestParam(name = "tableName", defaultValue = "setting_params") String tableName) {
        return APIResult.success(tableService.getTableColumns(tableName));
    }

    @PostMapping("/gen")
    public APIResult wistGenerate(
            @RequestParam(name = "projectPath",defaultValue = "/Users/liul/deploy/gen") String projectPath,
            @RequestParam(name = "packagePrefix", defaultValue = "the.workshop.milktea") String packagePrefix,
            @RequestParam(name = "webProjectPath",defaultValue = "/Users/liul/deploy/gen") String webProjectPath,
            @RequestParam(name = "webPackagePrefix",defaultValue = "the.workshop.milktea") String webPackagePrefix,

            @RequestParam(name = "tableName", defaultValue = "uc_dept") String tableName,
            @RequestParam(name = "tableNameRegex", defaultValue = "_") String tableNameRegex,
            @RequestParam(name = "moduleName", defaultValue = "uc") String moduleName,
            @RequestParam(name = "entityDescr", defaultValue = "部门") String entityDescr,

            @RequestParam(name = "author", defaultValue = "huamu") String author,

            @RequestParam(name = "addFileds", required = false) String addFileds,
            @RequestParam(name = "modifyFileds", required = false) String modifyFileds,
            @RequestParam(name = "detailFileds", required = false) String detailFileds,
            @RequestParam(name = "conditionFileds", required = false) String conditionFileds,
            @RequestParam(name = "listFileds", required = false) String listFileds,

            @RequestParam(name = "tpls", required = false) List<String> tpls
    ) {
        WistData data = new WistData();

        data.setUrl(druidProperties.getUrl());
        data.setUserName(druidProperties.getUsername());
        data.setPassword(druidProperties.getPassword());
        data.setDriverName(druidProperties.getDriverClassName());

        data.setProjectPath(projectPath);
        data.setPackagePrefix(packagePrefix);
        data.setWebProjectPath(webProjectPath);
        data.setWebPackagePrefix(webPackagePrefix);
        data.setTableName(tableName);
        data.setTableNameRegex(tableNameRegex);
        data.setModuleName(moduleName);
        data.setEntityDescr(entityDescr);
        data.setAuthor(author);

        if (StringUtils.hasText(addFileds)) {
            data.setAddFields(JSON.parseArray(addFileds, WistField.class));
        }
        if (StringUtils.hasText(modifyFileds)) {
            data.setModifyFields(JSON.parseArray(modifyFileds, WistField.class));
        }
        if (StringUtils.hasText(detailFileds)) {
            data.setDetailFields(JSON.parseArray(detailFileds, WistField.class));
        }
        if (StringUtils.hasText(conditionFileds)) {
            data.setConditionFields(JSON.parseArray(conditionFileds, WistField.class));
        }
        if (StringUtils.hasText(listFileds)) {
            data.setListFields(JSON.parseArray(listFileds, WistField.class));
        }

        WistEngine wistEngine = new WistEngine(data, tpls);
        wistEngine.doGeneration();

        return APIResult.success();
    }
}
